package com.ranshiyan.utils;

import java.io.UnsupportedEncodingException;
import java.util.Random;

/**
 *  随机类
 * @author zhuzg
 *
 */
public class CmsRandom {
	
	//存储用于生成随机字符串的集合 
	private static char  charNumChar[] = new char[36];
	static {
		int index=0;
		for(char i='0';i<='9';i++) {
			charNumChar[index++]=i;
		}
		for(char i='a';i<='z';i++) {
			charNumChar[index++]=i;
		}
	}
	

	/**
	 * 随机生成长度为n的字符串
	 * @param len 
	 * @return
	 */
	public static String randomEnStr(int len) {
		
		//
		StringBuilder sb = new StringBuilder();
		///String s = new String();
		Random random = new Random();
		
		for (int i = 0; i < len; i++) {
			int randomInt = random.nextInt(26);
			char c = (char)('a' + randomInt);
			sb.append(c);
			//s = s+c;
		}
		return sb.toString();	
	}
	
	/**
	 * 随机 n 位随机英文和数字字符串
	 * @param len
	 * @return 返回的字符串
	 */
	public static String randomStrEnNum(int len) {
		// 用于存储随机字符串
		StringBuilder sb = new StringBuilder();
		
		Random random = new Random();
		for (int i = 0; i < len; i++) {
			// 随机生成数组的下标
			int nextInt = random.nextInt(charNumChar.length);
			sb.append(charNumChar[nextInt]);
		}
		return sb.toString();
	}
	
	/**
	 * 随机生成一个汉字的字符串
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	 private static  String randomCnChar() throws UnsupportedEncodingException {
		
		   byte word[] = new byte[2];
			//  0x1A   0x1A+94
			Random random = new Random();
			word[0] = (byte)(0xA1 + 0x10 + random.nextInt(39));
			word[1] = (byte)(0xA1  + random.nextInt(94));
			return new String(word,"GBK");
			
	 }
	 
	 /**
	  * 生成长度为n的中文字符串
	  * @param len
	  * @return
	 * @throws UnsupportedEncodingException 
	  */
	 public static String RandomCh(int len) throws UnsupportedEncodingException {
		 
		 StringBuilder sb = new StringBuilder();
		 for (int i = 0; i < len; i++) {
			 // 随机生成一个汉字
			String  c = randomCnChar();
			sb.append(c);
			
		}
		 return sb.toString();
		 
	 }
	 
	
	
		
}
